/**
 * 大转盘界面
 */
class AwardUI extends UIBase{
    private btn_start:BaseButton;
    private group_award:eui.Group;
    private lb_jump:eui.Label;

    private award_1:eui.Image;
    private award_2:eui.Image;
    private award_3:eui.Image;
    private award_4:eui.Image;
    private award_5:eui.Image;
    private award_6:eui.Image;

    private lb_award_1:eui.Image;
    private lb_award_2:eui.Image;
    private lb_award_3:eui.Image;
    private lb_award_4:eui.Image;
    private lb_award_5:eui.Image;
    private lb_award_6:eui.Image;

    private deg: number;
	private duration: number;
    public static NUM:number = 6;
    private aim_id:number;
    private award_list = {};
    
    constructor()
    {
        super('AwardView',{showBG:true});
    }

    public init()
    {
        super.init(true);
        this.requestInfo();
    }

    public addEvent()
    {
        this.btn_start.addTapHandler(this.startHandle, this);
        this.lb_jump.addEventListener(egret.TouchEvent.TOUCH_TAP,this.jumpHandle, this);
    }

    public requestInfo()
    {
        let data = {
            version:GameConst.version
        }
        // UIManager.instance().showNormalLoading();
        HttpManager.instance().sendPost(GameConst.httpUrl + 'warriorother/lucky_wheel', StringUtil.jsonToHttpPostString(data), (e)=>{
            // UIManager.instance().hideNormalLoading();
            let data = JSON.parse(e)
            if(data.msg == 'ok')
            {
                this.award_list = data.data;
                this.initUI();
            }
            else
            {
                MessageUtil.tips('网络离家出走了');
            }
        }, this);
    }

    public initUI()
    {
        for(let k in this.award_list)
        {
            let award = this.award_list[k];
            let award_icon = this['award_'+k] as eui.Image;
            let award_label = this['lb_award_'+k] as eui.Label;
            let awardData = ResourceUtil.instance().parseSignleAward(award);
            if(awardData)
            {
                award_icon.source = awardData.itemIcon;
                let num = awardData.itemNum;
                award_label.text = `x${num}`;
                if(k == '6' && awardData.itemId > 5)
                {
                    award_icon.rotation = 0;
                    award_icon.width = award_icon.texture.textureWidth;
                    award_icon.height = award_icon.texture.textureHeight;
                    award_icon.scaleX = award_icon.scaleY = 0.6;
                }
            }
        }
    }

    protected startHandle()
    {
        this.touchChildren = false;
        this.getGift();
    }

    // 通信部分逻辑 获取中奖结果
    private getGift(): void {
        // 测试
        // this.onPostComplete();
        
        let data = {
            version:GameConst.version,
            award_list:JSON.stringify(this.award_list)
        }
        // UIManager.instance().showNormalLoading();
        HttpManager.instance().sendPost(GameConst.httpUrl + 'warriorother/do_lucky_wheel', StringUtil.jsonToHttpPostString(data), (e)=>{
            // UIManager.instance().hideNormalLoading();
            let data = JSON.parse(e)
            if(data.msg == 'ok')
            {
                this.aim_id = data.data;
                this.onPostComplete();
            }
            else
            {

            }
        }, this);
    }

    private onPostComplete(): void {
        //目标
        // let prize_id = Math.floor(Math.random() * AwardUI.NUM) + 1; // res.prize_id
        let prize_id = this.aim_id;
        // prize_id = Math.floor(Math.random()*Lottery.NUM);
        this.deg = 12 * 360 - (2 * prize_id - 1) * 30 + 60;
        //持续转动时间
        this.duration = this.calcDuration(this.deg);
        return this.startTurn(() => {
           
        }); // 启动转盘
    }

        // 根据角度计算持续时间 deg -> duration
    private calcDuration(deg: number): number {
        if(deg * 10 > 6000)
        return 6000;
        return deg * 10;
    }

	// 抽奖动画
	private startTurn(cb?: Function): void {
		egret.Tween.get(this.group_award)
			//.to({ rotation: this.inCir.rotation - 20 }, 500, egret.Ease.sineIn)
			.to({ rotation: this.deg }, this.duration, egret.Ease.quartInOut).call(() => {
				this.touchChildren = true;
				let award = this.award_list[this.aim_id];
                let self = this;
                GameUtil.openAwardUI(award, self.remove, this);
			});
	}

    protected jumpHandle()
    {
        ScreenMovies.MovieStart(3,()=>{
            this.remove(0);
        },this);
    }

    public removeEvent()
    {
        this.btn_start.removeTapHandler();
        this.lb_jump.removeEventListener(egret.TouchEvent.TOUCH_TAP,this.jumpHandle, this);
    }

    public clear()
    {
        super.clear();
        this.award_list = null;
        this.aim_id = null;
    }
}